﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Web;

namespace G.Service
{
    /// <summary>
    /// PushTestService 的摘要说明
    /// </summary>
    public class PushTestService : BaseHandler
    {

        //public void ProcessRequest(HttpContext context)
        //{
        //    context.Response.ContentType = "text/plain";
        //    //context.Response.Write("Hello World");
        //    var groups = GetWin8Config();
        //    DataContractJsonSerializer de = new DataContractJsonSerializer(groups.GetType());
        //    System.IO.MemoryStream stream=new System.IO.MemoryStream();
        //    de.WriteObject(stream, groups);
        //    stream.Seek(0, System.IO.SeekOrigin.Begin);
        //    using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
        //    {
        //        string str = reader.ReadToEnd();
        //        context.Response.Write(str);
        //    }
        //   //System.Runtime.Serialization.j
        //}

        #region GetWin8Config
        public Data.Group<G.Service.Data.Win8PushTestData>[] GetWin8Config()
        {
            Data.Group<G.Service.Data.Win8PushTestData>[] gs = new Data.Group<Data.Win8PushTestData>[2];
            gs[0] = new Data.Group<Data.Win8PushTestData>() { Name = "Toast", Items = GetFactoryInfos(typeof(Push.Win8.ToastContent.ToastContentFactory)).ToArray() };
            gs[1] = new Data.Group<Data.Win8PushTestData>() { Name = "Tile", Items = GetFactoryInfos(typeof(Push.Win8.TileContent.TileContentFactory)).ToArray() };
            return gs;
        }

        private IEnumerable<G.Service.Data.Win8PushTestData> GetFactoryInfos(Type factoryType)
        {
            foreach (var m in factoryType.GetMethods())
            {
                if (m.ReturnType.GetInterface("G.Service.Push.Win8.INotificationContent") == typeof(G.Service.Push.Win8.INotificationContent))
                {
                    G.Service.Data.Win8PushTestData d = new G.Service.Data.Win8PushTestData();
                    d.MethodName = m.Name;
                    d.MethodReturnType = m.ReturnType.FullName;
                    d.Win8PushTestContentProperties = GetWin8PushTestContentPropertys(m).ToArray();
                    yield return d;
                }
            }
        }

        private IEnumerable<Data.Win8PushTestContentProperty> GetWin8PushTestContentPropertys(System.Reflection.MethodInfo m)
        {
            foreach (var item in m.ReturnType.GetProperties())
            {
                G.Service.Data.Win8PushTestContentProperty p = new G.Service.Data.Win8PushTestContentProperty();
                p.Name = item.Name;
                p.TypeName = item.PropertyType.FullName;
                p.Properties = GetProprtysKVPair(item).ToArray();
                yield return p;
            }
        }

        private IEnumerable<Data.KeyValuePair> GetProprtysKVPair(System.Reflection.PropertyInfo item)
        {
            foreach (var pro in item.PropertyType.GetProperties())
            {
                yield return new G.Service.Data.KeyValuePair() { Key = pro.Name, Value = pro.PropertyType.FullName };
            }
        }
        #endregion

        protected override void ProcessRequest(HttpContext context, out BLL.IResult result, string action)
        {
            switch (action)
            {
                case "getwin8pushconfig":
                    G.Service.BLL.Result<G.Service.Data.Group<G.Service.Data.Win8PushTestData>[]> re = new G.Service.BLL.Result<G.Service.Data.Group<G.Service.Data.Win8PushTestData>[]>();
                    re.ResponseData = GetWin8Config();
                    re.IsOK = true;
                    result = re;
                    break;
                case "updatepushinfo":
                    {
                        string deviceid = QueryString["deviceid"];
                        string plateform = QueryString["platform"];
                        string pushurl = QueryString["pushurl"];
                        result = BLL.PushInfoBLL.UpdatePushInfo(deviceid, plateform, pushurl);
                    }
                    break;
                case "getallpushinfo":
                    result = BLL.PushInfoBLL.GetALLPushInfos();
                    break;
                default:
                    result = new BLL.Result() { Msg = "指定的action无效！" };
                    break;
            }


        }
    }
}